package com.example.bookexchange;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class ProvinceSelectedListener implements AdapterView.OnItemSelectedListener {

    private final Context context;
    private final Spinner spinnerCity;
    private final Spinner spinnerDistrict;

    public ProvinceSelectedListener(Context context, Spinner spinnerCity, Spinner spinnerDistrict) {
        this.context = context;
        this.spinnerCity = spinnerCity;
        this.spinnerDistrict = spinnerDistrict;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        int cityArrayId;
        switch (position) {
            case 1: // 广东省
                cityArrayId = R.array.guangdong_cities;
                break;
            case 2: // 浙江省
                cityArrayId = R.array.zhejiang_cities;
                break;
            default: // 默认
                cityArrayId = R.array.default_cities;
        }

        ArrayAdapter<CharSequence> cityAdapter = ArrayAdapter.createFromResource(
                context, cityArrayId, android.R.layout.simple_spinner_item);
        cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerCity.setAdapter(cityAdapter);

        // 重置区县
        ArrayAdapter<CharSequence> districtAdapter = ArrayAdapter.createFromResource(
                context, R.array.default_districts, android.R.layout.simple_spinner_item);
        districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerDistrict.setAdapter(districtAdapter);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {}
}
